package cn.lanqiao.active;

import cn.lanqiao.entity.Student;

import java.io.*;

public class FileDemo {
    public static void main(String[] args) {
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream("/Users/David/Desktop/data"));
            objectInputStream = new ObjectInputStream(new FileInputStream("/Users/David/Desktop/data"));
            //写user过程
            Student user = new Student(1001, "david");
            objectOutputStream.writeObject(user);
            objectOutputStream.flush();
            Student user1 = (Student) objectInputStream.readObject();
            System.out.println(user1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }BufferedInputStream bin = null;
        BufferedOutputStream bout = null;
        try {
            long begin = System.currentTimeMillis();
            bin = new BufferedInputStream(new FileInputStream("/Users/David/Desktop/软件设计师教程（第5版）.pdf"));
            bout = new BufferedOutputStream(new FileOutputStream("/Users/David/Desktop/软件设计师教程（第5版）-2.pdf"));// 加入true可以实现追加功能。
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = bin.read(bytes)) != -1) {
                bout.write(bytes, 0, len);
            }
            long end = System.currentTimeMillis();
            System.out.println(end - begin);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bin.close();
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
